import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import { fileURLToPath, URL } from "node:url";

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    plugins: [react()],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    base: "/bykj/",
    optimizeDeps: {
      include: ["qrcode", "jsbarcode"],
      exclude: ["html5-qrcode"],
    },
    build: {
      commonjsOptions: {
        include: [/qrcode/, /jsbarcode/, /node_modules/],
      },
    },
    server: {
      proxy: {
        "/api": {
          target: env.VITE_API_BASE,
          changeOrigin: true,
          secure: false,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
      open: true,
      port: 3000,
      host: "0.0.0.0",
    },
  };
});
